<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    for (i = 0; i < 10; i++) {
        var b = document.createElement("button")
        b.innerHTML = "b" + i
        // 方法1直接声明
        // b.onclick = function () {
        //     alert("这是" + this.innerHTML)
        // }

        // 方法2this调用事件所属对象
        // b.onclick = show

        // 方法3闭包传参
        b.onclick = show2(i)
        document.body.appendChild(b)
    }
    function show() {
        alert("这是" + this.innerHTML)
    }
    function show2(i) {
        return function () {
            alert(i)
        }
    }
</script>

</html>